/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.alibaba.fastjson.JSON;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Questiontype;
import me.zhengjie.modules.system.domain.QuestiontypeDo;
import me.zhengjie.modules.system.service.QuestiontypeService;
import me.zhengjie.modules.system.domain.vo.QuestiontypeQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.utils.TreeDataUtil.*;


/**
* @author shitou
* @date 2024-04-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "试题类型管理")
@RequestMapping("/api/questiontype")
public class QuestiontypeController {

    private final QuestiontypeService questiontypeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('questiontype:list')")
    public void exportQuestiontype(HttpServletResponse response, QuestiontypeQueryCriteria criteria) throws IOException {
        questiontypeService.download(questiontypeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询试题类型")
    @ApiOperation("查询试题类型")
//    @PreAuthorize("@el.check('questiontype:list')")
    public ResponseEntity<PageResult<Questiontype>> queryQuestiontype(QuestiontypeQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(questiontypeService.queryAll(criteria,page),HttpStatus.OK);
    }

//    QuestiontypeDo
    @GetMapping("/queryQuestiontypeTree")
    public ResponseEntity<Object> queryQuestiontypeTree(QuestiontypeQueryCriteria criteria){

        List<Questiontype> list1 = questiontypeService.queryAll(criteria);

        List<Map<String,Object>> list3 = new ArrayList<>();
        for(Questiontype queryType :list1){
            Map<String, Object> map = convertObjectToMap(queryType);
            map.put("id",queryType.getId());
            map.put("childList",new ArrayList<>());
            list3.add(map);
        }

        List<Map<String,Object>> list4 = list3.stream().filter(m -> m.get("parentId") == null).peek(
                (m) -> m.put("childList", getChildren(m,list3))
        ).collect(Collectors.toList());
        return new ResponseEntity<>(getResultList(list4),HttpStatus.OK);
    }

    @GetMapping("/queryQuestiontypeSchoolTree")
    public ResponseEntity<Object> queryQuestiontypeSchoolTree(QuestiontypeQueryCriteria criteria){

        List<Questiontype> list1 = questiontypeService.queryQuestiontypeSchoolTree(criteria);

        List<Map<String,Object>> list3 = new ArrayList<>();
        for(Questiontype queryType :list1){
            Map<String, Object> map = convertObjectToMap(queryType);
            map.put("id",queryType.getId());
            map.put("childList",new ArrayList<>());
            list3.add(map);
        }

        List<Map<String,Object>> list4 = list3.stream().filter(m -> m.get("parentId") == null).peek(
                (m) -> m.put("childList", getChildren(m,list3))
        ).collect(Collectors.toList());
        return new ResponseEntity<>(getResultList(list4),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增试题类型")
    @ApiOperation("新增试题类型")
//    @PreAuthorize("@el.check('questiontype:add')")
    public ResponseEntity<Object> createQuestiontype(@Validated @RequestBody Questiontype resources){
        questiontypeService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改试题类型")
    @ApiOperation("修改试题类型")
//    @PreAuthorize("@el.check('questiontype:edit')")
    public ResponseEntity<Object> updateQuestiontype(@Validated @RequestBody Questiontype resources){
        questiontypeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除试题类型")
    @ApiOperation("删除试题类型")
//    @PreAuthorize("@el.check('questiontype:del')")
    public ResponseEntity<Object> deleteQuestiontype(@RequestBody List<Integer> ids) {
        questiontypeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}